package com.hwua.tonyshop.controller.fg;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hwua.tonyshop.criteria.UserCriteria;
import com.hwua.tonyshop.entity.mojo.GoodsInfoAO;
import com.hwua.tonyshop.entity.mojo.UserAO;
import com.hwua.tonyshop.entity.pojo.User;
import com.hwua.tonyshop.service.IUserService;
import com.slkj.autocode.entity.ServiceResult;
import com.slkj.autocode.util.DateTimeUtils;
import com.slkj.autocode.util.StringUtils;

@Controller
@RequestMapping(value = "/fore/user")
public class UserController {
	@Resource
	IUserService userService;
		/**
		 * 跳转到登录界面
		 * @param model
		 * @param request
		 * @param response
		 * @return
		 */
		@RequestMapping(value = "/toLoginPage", method = { RequestMethod.GET, RequestMethod.POST })
		public String toLoginPage(Model model,HttpServletRequest request,HttpServletResponse response) {
		  	
			return "foreground/login/login";
		}
		/**
		 * 跳转到注册界面
		 * @param model
		 * @param request
		 * @param response
		 * @return
		 */
		@RequestMapping(value = "/toRegistPage", method = { RequestMethod.GET, RequestMethod.POST })
		public String toRegistPage(Model model,HttpServletRequest request,HttpServletResponse response) {
		  	
			return "foreground/login/register";
		}
		
		//判断某个账号是否存在
		@RequestMapping(value = "/getUserByUserName", method = { RequestMethod.GET, RequestMethod.POST })
		@ResponseBody
		public ServiceResult<List<UserAO>> getUserByUserName(String userName,String backToken,Model model,HttpServletRequest request,HttpServletResponse response) {
			ServiceResult<List<UserAO>> result = new ServiceResult<List<UserAO>>();
			if(StringUtils.isBlank(userName)){
				result.setCode(10001);
				result.setData(null);
				result.setMsg("账号不能为空");
				result.setSucceed(true);
			}else{
				UserCriteria criteria = new UserCriteria();
				criteria.andUserNameEquals(userName);
				result = userService.selectByCriteria(criteria);
			}
			return result;
		}
		//用户注册
		@RequestMapping(value = "/addUser", method = { RequestMethod.GET, RequestMethod.POST })
		@ResponseBody
		public ServiceResult<Integer> addUser(User user,String backToken,Model model,HttpServletRequest request,HttpServletResponse response) {
			ServiceResult<Integer> result = new ServiceResult<Integer>();
			if(StringUtils.isBlank(user.getUserName())||StringUtils.isBlank(user.getPassword())){
				result.setCode(10001);
				result.setData(null);
				result.setMsg("账号密码不能为空");
				result.setSucceed(true);
			}else{
				user.setState("0");
				user.setCreateDate(DateTimeUtils.formateDateToStr(new Date()));
				result = userService.addUser(user);
			}
			return result;
		}
		//用户登录
		@RequestMapping(value = "/userLogin", method = { RequestMethod.GET, RequestMethod.POST })
		@ResponseBody
		public ServiceResult<List<UserAO>> userLogin(User user,String backToken,Model model,HttpServletRequest request,HttpServletResponse response) {
			ServiceResult<List<UserAO>> result = new ServiceResult<List<UserAO>>();
			if(StringUtils.isBlank(user.getUserName())||StringUtils.isBlank(user.getPassword())){
				result.setCode(10001);
				result.setData(null);
				result.setMsg("账号密码不能为空");
				result.setSucceed(true);
			}else{
				UserCriteria criteria = new UserCriteria();
				criteria.andUserNameEquals(user.getUserName()).andPasswordEquals(user.getPassword()).andStateEquals("0");
				result = userService.selectByCriteria(criteria);
				if(result.getData()!=null&&result.getData().size()>0){
					String token = request.getSession().getId();
					request.getSession().setAttribute(token, result.getData().get(0));
					result.setAdditionalProperties("token", token);
				}
			}
			return result;
		}
}
